package cs2340.flyingkiwi.todoapp;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * Handles the Filter page and filter abilities.
 * @author Group Flying Kiwi
 * @version 30 Mar 2012
 */
public class Filter extends ListActivity{
	
	private ToDoAppDbAdapter mDbHelper;
	private DatePicker mDuedatePicker;
	
	/**
	 * Filters items by All, Category, Date, and Completed.
	 * @param savedInstanceState
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.filter);
		
		mDbHelper = new ToDoAppDbAdapter(this);
		mDbHelper.open();
		
		Log.d("Filter", "Databased opened");
		
		Button showAllButton = (Button) findViewById(R.id.filter_showAll);
		Button dateButton = (Button) findViewById(R.id.filter_date);
		Button categoriesButton = (Button) findViewById(R.id.filter_categories);
		Button incompletedButton = (Button) findViewById(R.id.filter_incompleted);
		Button completedButton = (Button) findViewById(R.id.filter_completed);
		Button mapButton = (Button) findViewById(R.id.map_button);
		//Button beforeButton = (Button) findViewById(R.id.dateFilter_before);
		//Button afterButton = (Button) findViewById(R.id.dateFilter_after);
		
		Log.d("Filter", "After declaring buttons");
		
		showAllButton.setOnClickListener(new View.OnClickListener() {
			
			/**
			 * Starts a new Activity to display all items when Show All button is clicked.
			 * @param v View
			 */
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Filter.this, Category.class);
		        startActivityForResult(i, 0);
				
				//setContentView(R.layout.filter_result);
				//fillDataWithAllItems();
		        //registerForContextMenu(getListView());
			}
		});
		
		Log.d("Filter", "showAllButton created");
		
		
		dateButton.setOnClickListener(new View.OnClickListener() {
			
			/**
			 * Displays datefilter page when Date button is clicked.
			 * @param v View
			 */
			@Override
			public void onClick(View v) {
				setContentView(R.layout.datefilter);
			}
		});
		
		Log.d("Filter", "dateButton created");
		
		categoriesButton.setOnClickListener(new View.OnClickListener() {
			
			/**
			 * Starts a new Activity to display Categories when Categories button is clicked.
			 * @param v View
			 */
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Filter.this, AccountManager.class);
				startActivityForResult(i, 0);
				
				//setContentView(R.layout.filter_result);
				//fillDataWithAllCategories();
				//registerForContextMenu(getListView());
			}
		});
		
		Log.d("Filter", "categoriesButton created");
		
		incompletedButton.setOnClickListener(new View.OnClickListener() {
			
			/**
			 * Displays not completed items in a ListView when Not Complete is clicked.
			 * @param v View
			 */
			@Override
			public void onClick(View v) {
				setContentView(R.layout.filter_result);
				fillDataWithCompletedItems("false");
				registerForContextMenu(getListView());
		
			}
		});
		
		Log.d("Filter", "incompletedButton created");
		
		/**
		 * Displays completed items in a ListView when Complete is clicked.
		 * @param v View
		 */
		completedButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setContentView(R.layout.filter_result);
				fillDataWithCompletedItems("true");
				registerForContextMenu(getListView());
			}
		});

		/**
		 * Displays completed items in a ListView when Complete is clicked.
		 * @param v View
		 */
		mapButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("Filter", "Inside button");
				Intent i = new Intent(Filter.this, KiWiMaps.class);
				startActivityForResult(i, 0);
			}
		});
		
		
		
		/*
		beforeButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setContentView(R.layout.filter_result);
				fillDataWithAllItems();
		        registerForContextMenu(getListView());
			}
		});
		
		afterButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setContentView(R.layout.filter_result);
				fillDataWithAllItems();
		        registerForContextMenu(getListView());
			}
		});
		*/
		Log.d("Filter", "All Buttons created");
		
	}
	
	/**
     * Retrieves either completed or not completed items from the database and 
     * constructs a list of the items.
     * @param completed String of whether retrieving complete or incomplete items
     */
	private void fillDataWithCompletedItems(String completed){
		Cursor itemsCursor = mDbHelper.fetchCompletedItems(completed);
        startManagingCursor(itemsCursor);

        String[] from = new String[]{ToDoAppDbAdapter.KEY_ITEM_TITLE};

        int[] to = new int[]{R.id.filter_result1};

        SimpleCursorAdapter items = 
            new SimpleCursorAdapter(this, R.layout.filter_result_row, itemsCursor, from, to);
        setListAdapter(items);
	}
	
	/**
	 * Retrieves all items from the database and determines which are before the 
	 * specified date. Constructs a list of those items.
	 * @param view View
	 */
	public void beforeButton(View view){
		Log.d("Filter", "Before button starts");
		mDuedatePicker = (DatePicker) findViewById(R.id.datePicker2);
		
		int month = mDuedatePicker.getMonth();
		int day = mDuedatePicker.getDayOfMonth();
		int year = mDuedatePicker.getYear();
		
		Log.d("Filter", "Date user input: " + month + " " + day + " " + year);
		
		Cursor itemCursor = mDbHelper.fetchAllItems();
		
		Log.d("Filter", "After database");
		String[] titles = new String[100];
		int i = 0;
		
		String date;
		String[] dateInfo;
		
		itemCursor.moveToFirst();
		
		while(!itemCursor.isAfterLast()){
			
			date = itemCursor.getString(4);
			dateInfo = date.split(" ");
			
			Log.d("Filter", "From database" + "Year: " + Integer.parseInt(dateInfo[2]) 
					+ " Month: " + Integer.parseInt(dateInfo[0]) 
					+ " Day: " + Integer.parseInt(dateInfo[1]));
			
			if(Integer.parseInt(dateInfo[2])<year){
				titles[i] = itemCursor.getString(1);
				i++;
			}
			else if(Integer.parseInt(dateInfo[0])<month){
				titles[i] = itemCursor.getString(1);
				i++;
			}
			else if(Integer.parseInt(dateInfo[1])<day){
				titles[i] = itemCursor.getString(1);
				i++;
			}
			else{}
			itemCursor.moveToNext();
		}
		
		Log.d("Filter", "Before button almost end");
		
		int j = 0;
		while(titles[j]!=null){
			Log.d("Filter", titles[j]);
			j++;
		}
		
		ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this,
				R.layout.filter_result, titles);
		
		//setContentView(R.layout.filter_result);
		
		setListAdapter(Adapter);
		//registerForContextMenu(getListView());
	}
	
	/**
	 * Retrieves all items from the database and determines which are after the 
	 * specified date. Constructs a list of those items.
	 * @param view View
	 */
	public void afterButton(View view){
		Log.d("Filter", "Before button starts");
		mDuedatePicker = (DatePicker) findViewById(R.id.datePicker2);
		
		int month = mDuedatePicker.getMonth();
		int day = mDuedatePicker.getDayOfMonth();
		int year = mDuedatePicker.getYear();
		
		Log.d("Filter", "Date user input: " + month + " " + day + " " + year);
		
		Cursor itemCursor = mDbHelper.fetchAllItems();
		
		Log.d("Filter", "After database");
		String[] titles = new String[100];
		int i = 0;
		
		String date;
		String[] dateInfo;
		
		itemCursor.moveToFirst();
		
		while(!itemCursor.isAfterLast()){
			
			date = itemCursor.getString(4);
			dateInfo = date.split(" ");
			
			Log.d("Filter", "From database" + "Year: " + Integer.parseInt(dateInfo[2]) 
					+ " Month: " + Integer.parseInt(dateInfo[0]) 
					+ " Day: " + Integer.parseInt(dateInfo[1]));
			
			if(Integer.parseInt(dateInfo[2])>year){
				titles[i] = itemCursor.getString(1);
				i++;
			}
			else if(Integer.parseInt(dateInfo[0])>month){
				titles[i] = itemCursor.getString(1);
				i++;
			}
			else if(Integer.parseInt(dateInfo[1])>day){
				titles[i] = itemCursor.getString(1);
				i++;
			}
			else{}
			itemCursor.moveToNext();
		}
		
		Log.d("Filter", "Before button almost end");
		
		int j = 0;
		while(titles[j]!=null){
			Log.d("Filter", titles[j]);
			j++;
			
		}
		
		ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this,
				R.layout.filter_result, titles);
		
		//setContentView(R.layout.filter_result);
		
		setListAdapter(Adapter);
		
		//registerForContextMenu(getListView());
	}
	
	/**
     * Interface definition for a class that will be called 
     * when the container's activity receives an activity result.
     * @param requestCode
     * @param resultCode
     * @param intent
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
    }
}
